<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 商品搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @change="getGoodsList" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品table表格区域 -->
      <el-table :data="goodslist" border stripe>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="70px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template v-slot="scope">
            {{scope.row.add_time|dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 商品分页栏 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.queryInfo.pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="this.queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request.js'
export default {
  name: 'List',
  data() {
    return {
      // 查询商品的列表的数据对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },

      // 商品数据
      goodslist: [],

      // 商品总数
      total: 0
    }
  },

  created() {
    this.getGoodsList()
  },

  // 监听器，当搜索框发生变化时，直接调用getGoodsList函数请求数据
  // watch: {
  //   'queryInfo.query': {
  //     handler() {
  //       this.getGoodsList()
  //     }
  //   }
  // },

  methods: {
    // 监听请求商品列表数据的事件
    async getGoodsList() {
      const { data: res } = await request.get(`goods`, { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品失败')
      }
      // 请求回来的商品数据
      this.goodslist = res.data.goods
      this.total = res.data.total
    },

    // 监听商品分页栏每页条数更改的事件
    handleSizeChange(val) {
      // 得到最新的每页条数，赋值给queryInfo.pagesize,请求商品数据
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },

    //监听商品分页栏页数更改的事件
    handleCurrentChange(val) {
      // 得到最新的页数，赋值给queryInfo.pagenum,请求商品数据
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },

    //监听删除商品事件
    async removeById(id) {
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (result !== 'confirm') {
        return
      }
      // 点击确定时发请求删商品
      const { data: res } = await request.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },

    // 监听添加商品事件
    goAddpage() {
      this.$router.push({ name: 'Add' })
    }
  }
}
</script>

<style lang="less" scoped >
</style>